﻿using System;
using Odyssey.Linq;
using SunTzu.Core.Data;

namespace Odyssey.Web.Models
{
    public static class DefaultCultureInfoProvider
    {
        private const string KEY_CULTURE = "user_lang";
        public static string CurrentCulture
        {
            get
            {
                var culture = CookieManager.Get(KEY_CULTURE);
                if (string.IsNullOrEmpty(culture))
                    return "zh_cn";
                return culture;
            }
            set { CookieManager.Set(KEY_CULTURE, value); }
        }
    }

    public static class CurrentCultureExtensions
    {
        public static string GetTitle(this tb_article entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.Title_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.Title_Zh_tw;
            else
                item = entity.Title_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Title_Zh_cn;
            return item;
        }
        public static string GetContent(this tb_article entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.Content_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.Content_Zh_tw;
            else
                item = entity.Content_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Content_Zh_cn;
            return item;
        }
        public static string GetKeyword(this tb_article entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.KeyWord_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.KeyWord_Zh_tw;
            else
                item = entity.KeyWord_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Content_Zh_cn;
            return item;
        }

        public static string GetName(this tb_topclass entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.Name_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.Name_Zh_tw;
            else
                item = entity.Name_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Name_Zh_cn;
            return item;
        }
        public static string GetName(this tb_subclass entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.Name_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.Name_Zh_tw;
            else
                item = entity.Name_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Name_Zh_cn;
            return item;
        }

        public static string GetPicUrl(this tb_topclass entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_Zh_tw;
            else
                item = entity.PicUrl_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.PicUrl_Zh_cn;
            return item;
        }
        public static string GetPicUrl(this tb_subclass entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_Zh_tw;
            else
                item = entity.PicUrl_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.PicUrl_Zh_cn;
            return item;
        }
        public static string GetPicUrl(this tb_article entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.PicUrl_Zh_tw;
            else
                item = entity.PicUrl_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.PicUrl_Zh_cn;
            return item;
        }
        public static string GetClassSummary(this tb_subclass entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.summary_en_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.summary_zh_tw;
            else
                item = entity.summary_zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.summary_zh_cn;
            return item;
        }

        public static string GetClassSummary(this tb_article entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.summary_en_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.summary_zh_tw;
            else
                item = entity.summary_zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.summary_zh_cn;
            return item;
        }

        public static string GetTitle(this ICulture entity)
        {
            return entity.GetTitle(DefaultCultureInfoProvider.CurrentCulture);
        }

        public static string GetResourceText(this string code)
        {
            var entity = CacheHelper<tb_resource_text>.GetAll().Find(it => it.Code.Equals(code, StringComparison.OrdinalIgnoreCase));
            if (entity == null) return string.Empty;
            return entity.GetTitle();
        }

        public static string GetProjectName(this tb_project entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectName_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectName_Zh_tw;
            else
                item = entity.ProjectName_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.ProjectName_Zh_cn;
            return item;
        }

        public static string GetProjectDescription(this tb_project entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectDescription_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectDescription_Zh_tw;
            else
                item = entity.ProjectDescription_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.ProjectDescription_Zh_cn;
            return item;
        }

        public static string GetImageUrl(this tb_project entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectImageUrl_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.ProjectImageUrl_Zh_tw;
            else
                item = entity.ProjectImageUrl_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.ProjectImageUrl_Zh_cn;
            return item;
        }

        public static string GetResourceImageUrl(this string code)
        {
            var entity = CacheHelper<tb_resource_image>.GetAll().Find(it => it.Code.Equals(code, StringComparison.OrdinalIgnoreCase));
            if (entity == null) return string.Empty;
            return entity.GetTitle();
        }

        public static string GetContent(this tb_custompage entity)
        {
            string item = null;
            if (DefaultCultureInfoProvider.CurrentCulture.Equals("en_us", StringComparison.OrdinalIgnoreCase))
                item = entity.Content_En_us;
            else if (DefaultCultureInfoProvider.CurrentCulture.Equals("zh_tw", StringComparison.OrdinalIgnoreCase))
                item = entity.Content_Zh_tw;
            else
                item = entity.Content_Zh_cn;

            if (string.IsNullOrWhiteSpace(item))
                return entity.Content_Zh_cn;
            return item;
        }
    }
}
